package com.wolfking.converter.transform.extend;

import com.google.gson.*;
import com.wolfking.converter.transform.AbstractExtendTransform;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

@Slf4j
public class JsonFormat4htmlTransform extends AbstractExtendTransform<Object> {

    private static final Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();

    @Override
    public Object transform(Object value) throws Exception {
        if (Objects.isNull(value)) {
            return value;
        }
        String s = value.toString();
        if (StringUtils.isBlank(s)) {
            return s;
        }
        try {
            JsonElement element = JsonParser.parseString(s);
            if (element.isJsonArray() || element.isJsonObject()) {
                reformat(element);
            }
            return gson.toJson(element)
                    .replace("\\r\\n\\t", "\n\t")
                    .replace("\\r\\n", "\n")
                    .replace("\\r\\t", "\t")
                    .replace("\\r", "")
                    .replace("\\t", "\t")
                    .replace("\\n", "\n")
                    .replace("\n", "<BR>")
                    .replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
                    .replace("\t\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
                    .replace(" ", "&nbsp;&nbsp;")
                    ;
        } catch (Exception e) {
            log.warn("", e);
            return value;
        }
    }

    private void reformat(JsonElement element) {
        if (element.isJsonObject()) {
            JsonObject object = element.getAsJsonObject();
            for (String key : object.keySet()) {
                JsonElement el = object.get(key);
                if (el.isJsonObject()) {
                    reformat(el.getAsJsonObject());
                } else if (el.isJsonArray()) {
                    reformat(el.getAsJsonArray());
                } else if (el.isJsonPrimitive()) {
                    String s = el.getAsJsonPrimitive().toString()
                            .replace("\\r\\n\\t", "")
                            .replace("\\r\\n", "")
                            .replace("\\r\\t", "")
                            .replace("\\r", "")
                            .replace("\\t", "")
                            .replace("\\n", "");
                    try {
                        JsonElement el2 = JsonParser.parseString(s);
                        if (el2.isJsonObject() || el2.isJsonArray()) {
                            reformat(el2);
                            object.remove(key);
                            object.add(key, el2);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

        } else if (element.isJsonArray()) {
            JsonArray array = element.getAsJsonArray();
            for (int i = 0; i < array.size(); i++) {
                reformat(array.get(i));
            }
        }
    }

    public static void main(String[] args) throws Exception {
        String s = "{\n" +
                "  \"dropDown\": \"\",\n" +
                "  \"expression\": \" and up.partner_loan_no = :fund_loan_key\",\n" +
                "  \"paramDescribe\": \"资金loanKey\",\n" +
                "  \"dropDownClass\": \"\",\n" +
                "  \"defaultValue\": \"\",\n" +
                "  \"converter\": \"com.wolfking.converter.transform.extend.MoreSqlQueryTransform\",\n" +
                "  \"format\": [\n" +
                "    {\n" +
                "      \"datasource\": \"fund\",\n" +
                "      \"sql\": \"select partner_loan_no from loan where loan_key = :arg\",\n" +
                "      \"paramName\": \"资金loanKey\",\n" +
                "      \"one\": true,\n" +
                "      \"returnEmpty\": false\n" +
                "    },\n" +
                "    {\n" +
                "      \"datasource\": \"proxy\",\n" +
                "      \"sql\": \"select partner_loan_no from `user_loan` where partner_id in(1,2,3,4,5,6,7,8) and proxy_serial_no = :arg\",\n" +
                "      \"paramName\": \"资金loanKey\",\n" +
                "      \"one\": true,\n" +
                "      \"returnEmpty\": false\n" +
                "    }\n" +
                "  ],\n" +
                "  \"pageSize\": \"false\",\n" +
                "  \"paramName\": \"fund_loan_key\",\n" +
                "  \"sort\": \" 5 \",\n" +
                "  \"pageNum\": \"false\",\n" +
                "  \"version\": \"9\",\n" +
                "  \"required\": \"false\",\n" +
                "  \"paramType\": \"java.lang.String\",\n" +
                "  \"defineKey\": \"proxy_repay\",\n" +
                "  \"id\": \"152\"\n" +
                "}";
        System.out.println(new JsonFormat4htmlTransform().transform(s));
    }
}
